package net.qhzw.vip.qiyu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import net.qhzw.vip.qiyu.entity.DtsUser;
import net.qhzw.vip.qiyu.entity.MallUserVip;
import net.qhzw.vip.qiyu.mapper.MallUserVipMapper;
import net.qhzw.vip.qiyu.service.MallUserVipService;
import net.qhzw.vip.qiyu.util.PageAdapter;
import net.qhzw.vip.qiyu.util.PageParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class MallUserVipServiceImpl extends ServiceImpl<MallUserVipMapper, MallUserVip>
        implements MallUserVipService {

    @Autowired
    private MallUserVipMapper mallUserVipMapper;

    @Override
    public int selectUserVip(Long userId, Long shopId) {
        int count = count(new LambdaQueryWrapper<MallUserVip>()
                .eq(MallUserVip::getUserId, userId)
                .eq(MallUserVip::getShopId, shopId));
        return count;
    }

    @Override
    public int getVIPbyProdId(Long userId, Long prodId) {
        return mallUserVipMapper.selectVIPbyProdId(userId, prodId);
    }

    @Override
    public void del(Long userId, Long shopId) {
        remove(new LambdaQueryWrapper<MallUserVip>()
                .eq(MallUserVip::getUserId, userId)
                .eq(MallUserVip::getShopId, shopId));
    }

    @Override
    public void setVIP(Long userId, Long shopId) {
        MallUserVip mallUserVip = new MallUserVip();
        mallUserVip.setId(IdWorker.getId());
        mallUserVip.setUserId(userId);
        mallUserVip.setShopId(shopId);
        save(mallUserVip);
    }

    @Override
    public IPage<DtsUser> findByPage(Long shopId, PageParam<DtsUser> page, String name, Integer isVip) {
        List<DtsUser> list = mallUserVipMapper.listMallUser(new PageAdapter(page), shopId, name, isVip);
        Long total = mallUserVipMapper.totalMallUser(shopId, name, isVip);
        page.setRecords(list);
        page.setTotal(total);
        return page;
    }
}
